﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RiskMan.DataClass.Models;
using System.Windows.Forms;
using XlDde;
using RiskMan.Views.Dialogs;
using TheControllerRisk.Settings;
using System.Reflection;

namespace RiskMan.Providers.Channels
{
    class DDEChannelTableLimiteByEmitent : XlDde.XlDdeChannel
    {
        private List<DataClass.Models.EmitentLimitModel> list;

        public DDEChannelTableLimiteByEmitent()
        {
        }
        protected override void ProcessTable(XlDde.XlTable xt, string topic)
        {
            list = RiskMan.MainWindow.StaticLedder._dataProvider.EmitentLimiteList;
            try
            {
                for (int i = 0; i < xt.Rows; i++)
                {
                    EmitentLimitModel elm = new EmitentLimitModel();
                    List<String> names = new List<String>();

                    for (int j = 0; j < xt.Columns; j++)
                    {
                        xt.ReadValue();
                        if (Namespace.Count == 0 && i == 0)
                        {
                            elm = null;
                            names.Add(xt.StringValue);
                        }
                        else
                        {
                            if ( IsSend( xt ) == true )
                            {
                                try
                                {
                                    if ( Namespace [ j ] == "FIRMID" ) elm.Emitent = xt.StringValue;
                                    if ( Namespace [ j ] == "SEC_SHORT_NAME" ) elm.NameSeccode = xt.StringValue;
                                    if ( Namespace [ j ] == "SECURITY" ) elm.Seccode = xt.StringValue;
                                    if ( Namespace [ j ] == "TRDACCID" ) elm.Account = xt.StringValue;
                                    if ( Namespace [ j ] == "CLIENT_CODE" ) elm.ClientCode = xt.StringValue;
                                    if ( Namespace [ j ] == "OPENBAL" ) elm.EnterOst = ( int ) xt.FloatValue;
                                    if ( Namespace [ j ] == "OPENLIMIT" ) elm.EnterLimit = ( int ) xt.FloatValue;
                                    if ( Namespace [ j ] == "CURRENTBAL" ) elm.CurrentOst = ( int ) xt.FloatValue;
                                    if ( Namespace [ j ] == "CURRENTLIMIT" ) elm.CurrentLimit = ( int ) xt.FloatValue;
                                    if ( Namespace [ j ] == "LOCKED" ) elm.Block = ( int ) xt.FloatValue;
                                    if ( Namespace [ j ] == "TOTAL" ) elm.Full = ( int ) xt.FloatValue;
                                    if ( Namespace [ j ] == "AVAILABLE" ) elm.Available = ( int ) xt.FloatValue;
                                    if ( Namespace [ j ] == "BALANCE" ) elm.Balance = ( int ) xt.FloatValue;
                                    if ( Namespace [ j ] == "WA_POSITION_PRICE" ) elm.PriceAcquisition = xt.FloatValue;
                                }
                                catch ( ArgumentOutOfRangeException )
                                {
                                    string message = "\"Таблица лимитов по бумагам\" в разных терминалах настроены по разному. \n\r" +
                                        "Для правильной настройки таблице ознакомьтесь с инструкцией." +
                                        "\n\rНеобходимо чтоб название и количество выводимых параметров совпадало.";
                                    
                                    string title = "Ошибка";

                                    if ( base.IsShowMessage == false )
                                    {
                                        base.IsShowMessage = true;
                                        new DialogMessage( message, title );
                                    }
                                    cfg.SetLog( message, RiskMan.MainWindow.StaticLedder.ListBox_Logs, GetType( ), MethodBase.GetCurrentMethod( ) );
                                }
                                catch ( Exception ex )
                                {
                                    new DialogMessage( ex.Message, "DDEChannelTableLimiteByEmitent" );
                                }
                            }
                            else
                            {
                                elm = null;
                            }
                        }
                    }
                    if (elm != null)
                    {
                        bool tr = false;
                        for (int r = 0; r < list.Count; r++)
                            if (list[r].Seccode == elm.Seccode && list[r].Account == elm.Account && list[r].ClientCode == elm.ClientCode)
                            {
                                tr = true;
                                list[r] = elm;
                                continue;
                            }

                        if (tr == false)
                            list.Add(elm);
                    }

                    if (names.Count > 0)
                        Namespace = new List<String>(names);
                }
            }
            catch (Exception ex)
            {
                new DialogOkCancel( ex.Message, "DDEChannelTableLimiteByEmitent" );
            }
        }

        public override void Disconnected(bool isConnected)
        {
            QuikIntegration.dde.QuikUtil.StartDDE();
            Namespace.Clear();
        }
    }
}
